import { defineStore } from 'pinia'
import $axios from '../utils/axios'
import { ElMessage } from 'element-plus'

export const useActivityStore = defineStore('activity', {
    state: () => {
        return {
            index: -1,
            dialogFormVisible: false,
            dialogFormTitle: '',
            aName: '',
            tableData: [],
            directorsData: [],//所有可选的活动负责人
            typesData: [],//可选活动类型
            activityTypeId: -1,
            selectedActivity: [],
            formdata: {
                id: -1
            },
            formdateCopy: {
                activityType: {},
                director: {}
            },
            activityDetail: {
                id: -1,
                pic: ''
            },
            currentPage: 1,
            pageSize: 3,
            total: '',
        }
    },
    actions: {
        async init() {
            const path = '/activity/findAll'
            const { data: res } = await $axios.get(path)
            if (res.code === 200) {
                this.tableData = res.result
                console.log(this.tableData);
            }
        },
        async selectAllByPage() {//分页初始化
            const path = '/activity/selectAllByPage/' + this.currentPage + '/' + this.pageSize
            const { data: res } = await $axios.get(path)
            if (res.code === 200) {
                this.tableData = res.result
                this.total = parseInt(res.msg)
                // console.log(this.tableData);
            }
        },
        async loadbyname() {//分页查询
            this.tableData.length = 0
            if (this.aName === '') {
                this.selectAllByPage()
            } else {
                const path = '/activity/selectByNameByPage/' + this.aName + '/' + this.currentPage + '/' + this.pageSize
                const { data: res } = await $axios.get(path)
                if (res.code === 200) {
                    this.tableData = res.result
                    this.total = parseInt(res.msg)
                }
            }
        },
        async selectByType(activityTypeId) {
            const path = '/activity/selectByType/' + activityTypeId
            const { data: res } = await $axios.get(path)
            if (res.code === 200) {
                // this.selectedActivity = res.result
                console.log(res.result);
                return res.result
            }
        },
        async loadDirector() {//加载所有可选的活动负责人
            const path = '/user/selectByRid/' + 2
            const { data: res } = await $axios.get(path)
            if (res.code === 200) {
                console.log(res.result)
                this.directorsData = res.result
            }
        },
        async loadType() {//加载活动类型
            const path = '/activityType/selectByState1'
            const { data: res } = await $axios.get(path)
            if (res.code === 200) {
                console.log(res.result)
                this.typesData = res.result
            }
        },
        preInfo4Add() {
            this.dialogFormTitle = '发布最新活动'
            this.dialogFormVisible = true
            this.loadDirector()
            this.loadType()
        },
        async addActivity() {//发布活动
            console.log(this.formdata);
            const path = '/activity/insert'
            const { data: res } = await $axios.put(path, this.formdata)
            if (res.code === 200) {
                this.dialogFormVisible = false
                ElMessage({
                    message: '成功！',
                    type: 'success',
                })
                this.selectAllByPage()
            }
            else {
                this.dialogFormVisible = false
                ElMessage.error(res.msg)
            }
        },
        preInfo4Edit(index, row) {
            this.index = index
            this.dialogFormTitle = '编辑活动内容'
            this.dialogFormVisible = true
            this.formdata = JSON.parse(JSON.stringify(row))
            this.formdateCopy = JSON.parse(JSON.stringify(row))
            this.loadDirector()
            this.loadType()
        },
        async editActivity() {
            console.log(this.formdata)
            const path = '/activity/update'
            const { data: res } = await $axios.post(path, this.formdata)
            if (res.code === 200) {
                // this.tableData.splice(this.index, 1, this.formdata)//当前索引位置替换掉一个
                this.dialogFormVisible = false
                ElMessage({
                    message: '修改成功！',
                    type: 'success',
                })
                this.selectAllByPage()
            } else {
                this.dialogFormVisible = false
                ElMessage.error(res.msg)
            }
        },
        save() {
            console.log(this.formdata.id);
            if (this.formdata.id == -1) {
                this.addActivity()
            }
            else {
                this.editActivity()
                console.log("edit");
                this.formdata = {}
                this.formdata.id = -1
                this.formdateCopy.activityType = {}
                this.formdateCopy.director = {}
                console.log(this.formdata);
                console.log(this.formdateCopy);
            }
            // this.init()
        },
        async initNotBegin() {
            const path = '/activity/selectNotBegin'
            const { data: res } = await $axios.get(path)
            if (res.code === 200) {
                this.tableData = res.result
                console.log(this.tableData);
            }
        },
    }
})